<?php
/*************************************
 *
 * @Controller 控制层主要接收与传递参数
 * 请求接口都需以下两个参数：
 *   @param String token 接口认证凭据
 *   @param String time 加密得到token时的时间戳
 *
 ************************************/
namespace app\api\controller;

use app\common\services\FligtServices;

use app\common\services\ImageServices;

use app\common\services\CodeMaServices;

use app\common\services\UserServices;
use think\Env;

use think\Request;

use think\Response;

use think\config;
use \think\exception\HttpException;

class Flight extends Base
{
    protected $fligtServices;

    protected $timeout = 10;

    protected $Request;
    protected $UserServices;

    public function __construct
    (
        Request $request
    )
    {
        parent::__construct($request);
        $this->fligtServices = new FligtServices();
        $this->UserServices = new UserServices();
    }

    /*
     *  提交订单
     *  @param Int f_id 航班id
     *  @param Int children_type 携带儿童 1：携带儿童 2：携带婴儿
     *  @param Int num 数量
     */
    public function addOrder(Request $request)
    {
        if(!Request::instance()->isPost()) {
            return json(['msg' => '非法请求！', 'status_code'=>400]);
        }
        $respons = $request->param();
        $sessionKey = $respons['session_key'];
        $checkLogin = $this->UserServices->checkLogin($sessionKey);
        if (!$checkLogin) {
            return json(['msg' => '暂未登陆', 'status_code'=>400]);
        }
        $respons['user_id'] = $checkLogin['id'];
        $msg = $this->UserServices->addOrder($respons);

        if ($msg) {
            return json(['msg' => '添加成功', 'status_code'=>200]);
        }
        return json(['msg' => '添加失败', 'status_code'=>400]);
    }

    /*
     *  航班查询
     *  @param String trip 去程(城市)
     *  @param String return_trip 回程(城市)
     */
    public function getFligt(Request $request)
    {
        $respons = $request->param();
        $check = $this->checkOust($respons);
        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $result = $this->fligtServices->getFligt($respons);

        if ($result) {
            return json(['data'=>$result, 'status_code'=>200],200);
        }
        return json(['data'=>'暂无数据', 'status_code'=>201]);
    }

    /*
     *  航班列表信息
     *  @param String tr_flight 去程航班号
     *  @param String trip 去程(城市)
     *  @param String return_trip 回程(城市)
     */
    public function getFligtList(Request $request)
    {
        $respons = $request->param();
        $check = $this->checkOust($respons);
        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $result = $this->fligtServices->getFligtList($respons);
        if ($result) {
            return json(['data'=>$result, 'status_code'=>200]);
        }
        return json(['data'=>'暂无数据', 'status_code'=>201]);
    }

    /*
     *  航班详情
     *  @param String session_key 登陆返回的字符串
     *  @param Int id 航班id
     */
    public function getFligtInfo(Request $request)
    {
        $respons = $request->param();
        $check = $this->checkOust($respons);
        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $result = $this->fligtServices->getFligtInfo($respons);

        if ($result) {
            return json(['data'=>$result, 'status_code'=>200], 200);
        }
        return json(['data'=>'暂无数据', 'status_code'=>201]);
    }


    /*
     *  查询服务-航班查询
     *  @param String session_key 登陆返回的字符串
     */
    public function getFligtTrip(Request $request)
    {
        $respons = $request->param();
        $check = $this->checkOust($respons);
        if (is_array($check)) {
            return $this->errorHttpResult(['data'=>$check['data'], 'status_code' => 400], 400);
        }
        $result = $this->fligtServices->getFligtTrip();
        if ($result) {
            return json(['data'=>$result, 'status_code'=>200], 200);
        }
        return json(['data'=>'暂无数据', 'status_code'=>201]);
    }

    /*
     *  虚拟订单接口
     *  @param String session_key 登陆返回的字符串
     *  @param String trip 去程(城市)
     *  @param String return_trip 回程(城市)
     *  @param String trip_time 去程时间
     *  @param String rt_time 回程时间
     *  @param Int type 1携带儿童 2：携带婴儿
     *  @param String flights 航班号
     */
    public function addVirtualOrder(Request $request)
    {
        if(!Request::instance()->isPost()) {
            return json(['msg' => '非法请求！', 'status_code'=>400]);
        }
        $respons = $request->param();
        $sessionKey = $respons['session_key'];
        $checkLogin = $this->UserServices->checkLogin($sessionKey);
        if (!$checkLogin) {
            return json(['msg' => '暂未登陆', 'status_code'=>400]);
        }
        if (empty($checkLogin['mobile'])) {
            return json(['msg' => '暂未绑定手机号码', 'status_code'=>401]);
        }
        $respons['user_id'] = $checkLogin['id'];
        $result = $this->fligtServices->addVirtualOrder($respons);
        if ($result) {
            return json(['data'=>'添加成功', 'status_code'=>200], 200);
        }
        return json(['data'=>'添加失败', 'status_code'=>400], 400);
    }
}